package cfca.paperless.client.servlet._8_local;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;

import cfca.paperless.client.bean.FieldBean;
import cfca.paperless.client.bean.ImageBean;
import cfca.paperless.client.bean.TextBean;
import cfca.paperless.client.servlet.PaperlessConfig;
import cfca.paperless.client.util.IoUtil;
import cfca.paperless.client.util.PdfOperateUtil;

/**
 * @Description PDF自动化合成业务数据接口，使用示例。
 * 1，操作对象为本地待处理的单个PDF文件，所有操作都在客户本地完成。
 * 2，合成内容包含：文本域内容、文本、图片和二维码。
 * 3，返回成功合成内容的PDF文件。
 * 备注：对于图片和二维码合成，只支持合成其图片数据。
 * 
 * @Author dell
 * @Date 2017-12-12
 * @CodeReviewer TODO
 */
public class SynthesizeAutoBusinessPdfLocalTest {

    public static void main(String[] args) throws Exception {

        System.out.println("SynthesizeAutoBusinessPdfLocalTest START");

        String pdfFilePath = "./TestData/tmp.pdf";
        String pdfSuffix = IoUtil.getFileNameSuffix(pdfFilePath);
        if(!PaperlessConfig.PDF_FILE_SUFFIX.equals(pdfSuffix)){
            throw new Exception("所选文件不是pdf文件");
        }
        
        // 待合成业务数据的PDF文件
        byte[] pdfFileData = FileUtils.readFileToByteArray(new File(pdfFilePath));

        // 需要合成到PDF文本域的业务数据，可为空
        List<FieldBean> checkboxFieldBeanList = null;
        List<FieldBean> fieldBeanList = getFieldBeanList();
        
        // 需要合成到PDF中的文本，可为空
        List<TextBean> textBeanList = getTextBeanList();

        // 需要合成到PDF中的图片和二维码，可为空
        List<ImageBean> imageBeanList = getImageBeanList();
        
        byte[] synthesizeResult = null;

        // 只合成文本
        //synthesizeResult = PdfOperateUtil.doSynthesizeAutoBusinessPdf(pdfFileData, textBeanList);
        // 只合成文本和图片
        //synthesizeResult = PdfOperateUtil.doSynthesizeAutoBusinessPdf(pdfFileData, textBeanList, imageBeanList);
        // 合成复选框、文本域、文本、图片
        synthesizeResult = PdfOperateUtil.doSynthesizeAutoBusinessPdf(pdfFileData, checkboxFieldBeanList, fieldBeanList, false, textBeanList, imageBeanList);
        
        System.out.println("SynthesizeAutoBusinessPdfLocalTest END OK");
        IoUtil.write("./TestData/output/SynthesizeAutoBusinessPdfLocalTest.pdf", synthesizeResult);
    }

    private static List<FieldBean> getFieldBeanList() throws Exception {
        List<FieldBean> fieldBeanList = new ArrayList<FieldBean>();

        fieldBeanList.add(new FieldBean("serialNo", "201712121739408898538024", "", "text"));
        fieldBeanList.add(new FieldBean("sequenceNo1", "20171212174008004", "", "text"));
        
        fieldBeanList.add(new FieldBean("cardNo1", "6226 1234 5678 5698", "", "text"));
        fieldBeanList.add(new FieldBean("accountName", "张三", "", "text"));
        fieldBeanList.add(new FieldBean("ICcardType", "借记卡", "", "text"));
        fieldBeanList.add(new FieldBean("identType", "身份证", "", "text"));
        fieldBeanList.add(new FieldBean("identNo", "110112201712125678", "", "text"));
        
        fieldBeanList.add(new FieldBean("branch", "北京分行亦庄支行", "", "text"));
        fieldBeanList.add(new FieldBean("operator", "李四", "", "text"));
        fieldBeanList.add(new FieldBean("date", "2017年12月12日", "", "text"));

        return fieldBeanList;
    }

    private static List<TextBean> getTextBeanList() throws Exception {
        List<TextBean> textBeanList = new ArrayList<TextBean>();
        
        TextBean textBean1 = new TextBean("你好啊，\r\n我是根据坐标进行合成的！", "", 1, 100, 100);// 指定文本，根据坐标定位进行合成
        textBean1.setTextFontSize("15");
        textBean1.setTextColor("FF0000");
        textBean1.setSpacingHeight(20);// 设置行间距，默认15像素
        
        textBeanList.add(textBean1);
        
        TextBean textBean2 = new TextBean("你好啊，\r\n我是根据关键字进行合成的！", "", "资料", "R", 10, 10);// 指定文本，根据关键字定位进行合成
        textBean2.setTextFontSize("15");
        textBean2.setTextColor("0000FF");
        textBean1.setSpacingHeight(20);// 设置行间距，默认15像素
        
        textBeanList.add(textBean2);

        return textBeanList;
    }

    private static List<ImageBean> getImageBeanList() throws Exception {
        List<ImageBean> imageBeanList = new ArrayList<ImageBean>();
        
        byte[] imageData = FileUtils.readFileToByteArray(new File("./TestData/zhangfei.png"));// 图片数据
      
        ImageBean imageBean1 = new ImageBean("100", "100", 1, "120", "120");// 传入图片数据，根据坐标定位进行合成
        imageBean1.setImageData(imageData);
        
        imageBeanList.add(imageBean1);
        
        ImageBean imageBean2 = new ImageBean("100", "100", "申请人/代理人签名", "D", 0, 0);// 传入图片数据，根据关键字定位进行合成
        imageBean2.setImageData(imageData);
        
        imageBeanList.add(imageBean2);

        return imageBeanList;
    }

}
